package org.aaron.wbclient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aaron.wbclient.utils.ApacheUtils;
import org.aaron.wbclient.utils.HttpUtils;
import org.aaron.wbclient.utils.OAuthUtils;
import org.aaron.wbclient.utils.StringUtils;
import org.aaron.wbclient.utils.UrlUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;
import android.webkit.URLUtil;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

/**
 * 腾讯微博客户端操作类
 * 
 * @author OuYangX
 * 
 */
public class WeiboClientOperate {

	private OAuthConsumer consumer;

	public WeiboClientOperate() {
	}

	public WeiboClientOperate(String consumerKey, String consumerSecret, String oauthTokenKey, String oauthTokenSecret) {
		consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
		consumer.setTokenWithSecret(oauthTokenKey, oauthTokenSecret);
	}

	public String doPost(String url, Map<String, String> additionalParams, List<String> decodeParams) {
		String result = "";
		HttpPost post = new HttpPost(url);
		consumer = OAuthUtils.addAddtionalParametersFromMap(consumer, additionalParams);
		try {
			consumer.sign(post);
		} catch (OAuthMessageSignerException e) {
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			e.printStackTrace();
		}
		Header header = post.getFirstHeader("Authorization");
		Log.d("Header Value", header.getValue());
		Map<String, String> oauthParams = StringUtils.parseMapFromString(header.getValue().substring(5).trim());
		oauthParams = HttpUtils.decodeByDecodeNames(decodeParams, oauthParams);
		additionalParams = HttpUtils.decodeByDecodeNames(decodeParams, additionalParams);
		List<NameValuePair> pairs = ApacheUtils.convertMapToNameValuePairs(oauthParams);
		List<NameValuePair> additionalpairs = ApacheUtils.convertMapToNameValuePairs(additionalParams);
		pairs.addAll(additionalpairs);
		HttpResponse response = null;
		try {
			HttpEntity entity = new UrlEncodedFormEntity(pairs);
			post.setEntity(entity);
			response = new DefaultHttpClient().execute(post);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (response != null) {
			result = ApacheUtils.getResponseText(response);
			Log.d("POST Response Text:", result);
		}
		return result;
	}

	public String doGet(String url, Map<String, String> additionalParams) {
		String result = "";
		url = UrlUtils.buildUrlByQueryStringMapAndBaseUrl(url, additionalParams);
		String signedUrl = null;
		try {
			signedUrl = consumer.sign(url);
		} catch (OAuthMessageSignerException e) {
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			e.printStackTrace();
		}
		HttpGet get = new HttpGet(signedUrl);
		HttpClient client = new DefaultHttpClient();
		HttpResponse response = null;
		try {
			response = client.execute(get);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (response != null) {
			result = ApacheUtils.getResponseText(response);
			Log.d("GET Response Text:", result);
		}
		return result;
	}
}
